//
//  ViewController.m
//  CoreAnimationDemo
//
//  Created by ZJ on 2018/8/11.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIAttachmentBehavior *attach;

@property (nonatomic, strong) NSMutableArray *bodys;


@end

@implementation ViewController

// 懒加载初始化
- (NSMutableArray *)bodys {
    if (_bodys == nil) {
        _bodys = [NSMutableArray array];
    }
    
    return _bodys;
}

- (UIDynamicAnimator *)animator {
    if (_animator == nil) {
        _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    }
    
    return _animator;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self setupGUI];
}

- (void)setupGUI {
    for (int i = 0; i < 9; i++) {
        UIView *wormView = [[UIView alloc] init];
        
        // 计算 frame
        CGFloat w = 30;
        CGFloat h = 30;
        CGFloat x = i * w;
        CGFloat y = 100;
        
        wormView.frame = CGRectMake(x, y, w, h); // frame
        wormView.backgroundColor = [UIColor redColor]; // 设置颜色
        
        // 圆角
        wormView.layer.cornerRadius = w * 0.5;
        
        if (i == 8) { // 头
            wormView.frame = CGRectMake(x, y - h * 0.5, w * 2, h * 2);
            wormView.backgroundColor = [UIColor blueColor];
            wormView.layer.cornerRadius = h;
            
            // 1.添加一个拖拽手势
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
            // 2.对某个view使用这个手势
            [wormView addGestureRecognizer:pan];
        }
        
        [self.view addSubview:wormView]; // 添加到控制器
        [self.bodys addObject:wormView];
    }
    
    for (int i = 0; i < self.bodys.count - 1; i++) {
        // 附着
        UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc] initWithItem:self.bodys[i] attachedToItem:self.bodys[i + 1]];
        [self.animator addBehavior:attach];
    }
    
    // 重力
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:self.bodys];
    [self.animator addBehavior:gravity];
    
    // 碰撞
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:self.bodys];
    // 把引用的 view 设置成边界
    collision.translatesReferenceBoundsIntoBoundary = YES;
    [self.animator addBehavior:collision];
}

// 3.实现拖拽手势的方法
- (void)pan:(UIPanGestureRecognizer *)sender {
    // 获取手指的位置
    CGPoint p = [sender locationInView:self.view];
    
    // 1.创建动画者对象
    // -见懒加载
    if (self.attach == nil) {
        UIAttachmentBehavior *att = [[UIAttachmentBehavior alloc] initWithItem:sender.view attachedToAnchor:p];
        self.attach = att;
    }
    
    // 修改手指的位置
    self.attach.anchorPoint = p;
    
    // 3.把行为添加到动画者对象当中
    [self.animator addBehavior:self.attach];
    
    // 撒手的时候
    if (sender.state == UIGestureRecognizerStateEnded) {
        // 移除掉 手指和头的附着行为
        [self.animator removeBehavior:self.attach];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
